/**************************************************************************************** 
 Copyright © 2003-2012 hbasesoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.hbasesoft.hamibot.job;

import java.io.File;
import java.util.Date;

import org.apache.commons.collections4.CollectionUtils;

import com.hbasesoft.framework.common.GlobalConstants;
import com.hbasesoft.framework.common.utils.ContextHolder;
import com.hbasesoft.framework.common.utils.date.DateUtil;
import com.hbasesoft.framework.common.utils.logger.LoggerUtil;
import com.hbasesoft.framework.db.TransactionUtil;
import com.hbasesoft.framework.db.core.utils.PagerList;
import com.hbasesoft.framework.job.core.JobContext;
import com.hbasesoft.framework.job.core.SimpleJob;
import com.hbasesoft.framework.job.core.annotation.Job;
import com.hbasesoft.hamibot.dao.AppLogDao;
import com.hbasesoft.hamibot.entity.AppLogEntity;

/**
 * <Description> <br>
 * 
 * @author 王伟<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2024年12月24日 <br>
 * @since V1.0<br>
 * @see com.hbasesoft.hamibot.job <br>
 */
@Job(cron = "0 0 0/1 * * ?")
public class DelLogJob implements SimpleJob {

    /** */
    private static final String FILE_STORAGE_PATH = GlobalConstants.FILE_STORAGE_PATH + "/hamibot";

    /** */
    private AppLogDao appLogDao;

    /**
     * Description: <br>
     * 
     * @author 王伟<br>
     * @taskId <br>
     * @param jobContext <br>
     */
    @Override
    public void execute(final JobContext jobContext) {
        AppLogDao dao = getAppLogDao();
        Date date = DateUtil.offset(DateUtil.HOUR, -1);

        LoggerUtil.info("准备开始执行删除日志");

        int i = 0;
        PagerList<String> datas = null;
        do {
            final int index = i++;
            datas = TransactionUtil.withSession((tm, ts) -> {
                return dao
                    .queryPagerByLambda(
                        q -> q.select(AppLogEntity::getScreenshot).lessThan(AppLogEntity::getCreateTime, date)
                            .isNotNull(AppLogEntity::getScreenshot),
                        index, GlobalConstants.DEFAULT_LINES, String.class);

            });

            if (CollectionUtils.isNotEmpty(datas)) {
                datas.forEach(data -> {
                    File file = new File(FILE_STORAGE_PATH + data);
                    if (file.exists()) {
                        file.delete();
                    }
                });
            }
        }
        while (datas != null && datas.hasNextPage());

        TransactionUtil.withSession((tm, ts) -> {
            dao.deleteByLambda(q -> q.lessThan(AppLogEntity::getCreateTime, date));
        });
        LoggerUtil.info("执行删除结束");

    }

    private AppLogDao getAppLogDao() {
        if (appLogDao == null) {
            appLogDao = ContextHolder.getContext().getBean(AppLogDao.class);
        }
        return appLogDao;
    }
}
